package com.itheima.controller;

import com.itheima.constrant.MessageConstant;
import com.itheima.constrant.RedisConstant;
import com.itheima.constrant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils1;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @author FCJ
 * @date 2020-01-07 18:33
 */
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
private JedisPool jedisPool;
@RequestMapping("/send4order")
    public Result send4Order(String telephone){
    String code = ValidateCodeUtils.generateValidateCode(4).toString();
    try{
//        SMSUtils1.sendSms(SMSUtils1.VALIDATE_CODE,telephone,code);
        System.out.println("验证码是："+code);
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,code);
   return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS,code);
    }catch(Exception e){
return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
    }
    }
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone){
        String code = ValidateCodeUtils.generateValidateCode(6).toString();
        try{
//        SMSUtils1.sendSms(SMSUtils1.VALIDATE_CODE,telephone,code);
            System.out.println(code);
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,300,code);
            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS,code);
        }catch(Exception e){
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
